package m.framework.web;

import java.util.Collection;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import m.framework.exception.SystemException;

/**request和map的适配器*/
public class RequestMap implements Map<String,String[]>{
	private HttpServletRequest request;
	
	public RequestMap(HttpServletRequest request){
		this.request=request;
	}
	
	public int size() {
		throw new SystemException("unimplemented");
	}
	public boolean isEmpty() {
		throw new SystemException("unimplemented");
	}
	public boolean containsKey(Object key) {
		throw new SystemException("unimplemented");
	}
	public boolean containsValue(Object value) {
		throw new SystemException("unimplemented");
	}
	public String[] get(Object key) {
		return request.getParameterValues((String) key);
	}
	public String[] put(String key, String[] value) {
		throw new SystemException("unimplemented");
	}
	public String[] remove(Object key) {
		throw new SystemException("unimplemented");
	}
	public void putAll(Map<? extends String, ? extends String[]> m) {
		throw new SystemException("unimplemented");
	}
	public void clear() {
		throw new SystemException("unimplemented");
	}
	public Set<String> keySet() {
		Set set=new HashSet();
		Enumeration e=request.getParameterNames();
		while(e.hasMoreElements()){
			set.add(e.nextElement());
		}
		return set;
	}
	public Collection<String[]> values() {
		throw new SystemException("unimplemented");
	}
	public Set<java.util.Map.Entry<String, String[]>> entrySet() {
		Set set=new HashSet();
		Enumeration e=request.getParameterNames();
		while(e.hasMoreElements()){
			String key=(String) e.nextElement();
			Entry en=new MyEntry(key);
			en.setValue(request.getParameterValues(key));
			set.add(en);
		}
		return set;
		
	}

	private class MyEntry implements Entry<String,String[]>{
		private String key;
		private String[] value;
		public MyEntry(String key){
			this.key=key;
		}

		public String getKey() {
			return key;
		}

		public String[] getValue() {
			return value;
		}

		public String[] setValue(String[] value) {
			String[] va=new String[value.length];
			for(int i=0;i<va.length;i++){
				va[i]=value[i];
			}
			this.value=va;
			return value;
		}
	}

	public HttpServletRequest getRequest(){
		return request;
	}
}
